package com.taotao.manage.controller;

import java.lang.reflect.Method;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageInfo;
import com.taotao.common.utils.EasyUIResult;
import com.taotao.manage.pojo.Item;
import com.taotao.manage.pojo.ItemDesc;
import com.taotao.manage.service.ItemDescService;
import com.taotao.manage.service.ItemService;

@RequestMapping("item")
@Controller
public class ItemController {
	
	@Autowired
	private ItemService itemService;
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class);

	//private static final List<Item> Item = null;
	
	/**
	 * 新增商品
	 * @param item
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST)
	public ResponseEntity<Void> saveItem(Item item, @RequestParam("desc") String desc,
				@RequestParam("itemParams")	String itemParams) {
		try {
			if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("新增商品！ item={},desc={}",item,desc);
			}
			//新增商品数据
			itemService.saveItem(item,desc,itemParams);
			if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("新增商品成功！ item={}",item);
			}
			//成功响应201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.error("新增商品出错！  item="+item,e);
			}
			//e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	/**
	 * 查询商品列表
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItem(@RequestParam(value="page",defaultValue="1")Integer page,
			@RequestParam(value="rows",defaultValue="30")Integer rows) {
		try {
			PageInfo<Item> pageInfo=itemService.queryPageListByUpdatedTimeDESC(page, rows);
			EasyUIResult easyUIResult = new EasyUIResult(pageInfo.getTotal(),pageInfo.getList());
			//成功响应200
			return ResponseEntity.status(HttpStatus.OK).body(easyUIResult);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
		}
	}
	
	/**
	 * 根据ids批量删除商品   (逻辑删除，不删库)
	 * @param item
	 * @return
	 */
	@RequestMapping(method=RequestMethod.DELETE)
	public ResponseEntity<Void> deleteItem(@RequestParam("ids") List<Object> ids) {
		try {
			if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("删除商品！ids ={}",ids);
			}
			//删除商品数据
			itemService.UpdateItemByIds(ids);
			if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("删除商品成功！ ids={}",ids);
			}
			//删除成功响应204
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.error("删除商品出错！  ids="+ids,e);
			}
			//e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	
	/**
	 * 根据ids批量下架商品   (商品状态该为2)
	 * @param item
	 * @return
	 */
	@RequestMapping(value="instock", method=RequestMethod.PUT)
	public ResponseEntity<Void> instockItem(@RequestParam("ids") List<Object> ids) {
		try {
			if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("下架商品！ids ={}",ids);
			}
			//更新商品数据
			itemService.instockItemByIds(ids);
			if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("下架商品成功！ ids={}",ids);
			}
			//下架成功响应201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.error("下架商品出错！  ids="+ids,e);
			}
			//e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	/**
	 * 根据ids批量上架商品   (商品状态该为1)
	 * @param item
	 * @return
	 */
	@RequestMapping(value="reshelf", method=RequestMethod.PUT)
	public ResponseEntity<Void>reshelfItem(@RequestParam("ids") List<Object> ids) {
		try {
			if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("上架商品！ids ={}",ids);
			}
			//更新商品数据
			Integer count = itemService.reshelfItemByIds(ids);
			if (count>0) {
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug("上架商品成功！ ids={}",ids);
					}
				//上架成功响应201
				return ResponseEntity.status(HttpStatus.CREATED).build();
			}else{
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug("上架商品成功！ ids={}",ids);
					}
				//上架失败，参数错误，返回400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.error("上架商品出错！  ids="+ids,e);
			}
			//e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	
	
	/**
	 * 修改商品   
	 * @param item
	 * @return
	 */
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(Item item, @RequestParam("desc") String desc,@RequestParam("itemParamId") Long itemParamId,
							@RequestParam("itemParams")	String itemParams) {
		try {
			if (LOGGER.isDebugEnabled()) {
				//{}为占位符
				LOGGER.debug("修改商品！ item={},desc={},itemParamId={}",item,desc,itemParamId);
			}
			//修改商品数据
			if (item==null) {
				if (LOGGER.isDebugEnabled()) {
					LOGGER.debug("修改商品参数出错！ item={}",item);
					}
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			itemService.updateItem(item,desc,itemParamId,itemParams);
			if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("修改商品成功！ item={}",item);
			}
			//成功响应201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.error("修改商品出错！  item="+item,e);
			}
			//e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
		/**
		 * 根据Id查询商品
		 * @param item
		 * @return
		 */
		@RequestMapping(value="{itemId}",method=RequestMethod.GET)
		public ResponseEntity<Item> getItemByItemId(@PathVariable("itemId") Long itemId) {
			try {
					Item item = this.itemService.queryById(itemId);
					if (item!=null) {
						return ResponseEntity.status(HttpStatus.OK).body(item);
					}
					return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
			} catch (Exception e) {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
			}
	}
	
}
